package com.mtconnect.listen;

import com.mtconnect.common.ValidateDB;
import com.mtconnect.exception.DAOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Created by Administrator on 2017/9/26.
 * 对系统数据库进行初始化.
 */
@WebListener
public class DBInitListener implements ServletContextListener{

    private static final Logger LOGGER = LoggerFactory.getLogger(DBInitListener.class);
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ValidateDB db = new ValidateDB();
        try {
            db.createTable();
        } catch (DAOException e) {
            LOGGER.error("database initialize failed~!",e);
            //初始化失败，则应用退出启动
            System.exit(0);
        }
        db.close();
        LOGGER.warn("web context initialize");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
